package com.school.graduation.detailed.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.model.Detailed;
import com.school.graduation.util.ToastUtil;
import com.school.graduation.view.SignInActivity;

import java.io.IOException;

import butterknife.Bind;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

public class DialogBoxActivityNew extends BaseActivity implements OnClickListener{
    @Bind(R.id.activity_bottom_dialogbox_layout)
    RelativeLayout layout;
    @Bind(R.id.activity_bottom_dialogbox_edit)
    EditText editText;
    @Bind(R.id.activity_bottom_dialogbox_send)
    TextView send;
    private String  productId;
    @Override
    public int getContentView() {
        return R.layout.activity_bottom_dialogbox;
    }

    @Override
    public void initData() {
        productId = getIntent().getStringExtra("product");
        initOnclickListener();
        setEditWatcher();

    }

    private void initOnclickListener(){
        layout.setOnClickListener(this);
    }

    private void setEditWatcher(){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() != 0){
                    final String str = s.toString();
                    send.setBackground(context.getResources().getDrawable(R.drawable.frame_red));
                    send.setTextColor(context.getResources().getColor(R.color.md_white_1000));
                    send.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try{
                                SharedPreferences sp = getApplicationContext().getSharedPreferences("user_cache", Context.MODE_MULTI_PROCESS |
                                        Activity.MODE_WORLD_WRITEABLE);
                                sendMessage(str,sp.getString("id",""));

                            }catch (NullPointerException e){
                                ToastUtil.makeShortToast(context,"请先登录");
                                Intent intent = new Intent(context,SignInActivity.class);
                                startActivity(intent);
                            }
                        }
                    });
                }else {
                    send.setBackground(context.getResources().getDrawable(R.drawable.frame));
                    send.setTextColor(context.getResources().getColor(R.color.md_blue_grey_500));
                    send.setOnClickListener(null);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void sendMessage(final String content, final String id){
        new Thread(new Runnable() {
            @Override
            public void run() {
                okhttp3.OkHttpClient client = new okhttp3.OkHttpClient();
                okhttp3.RequestBody body = new FormBody.Builder()
                        .add("product",productId)
                        .add("from", id)
                        .add("content",content)
                        .build();
                Request request = new Request.Builder()
                        .url("http://123.206.20.29:3000/user/comment")
                        .post(body)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("data","fail");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String str = response.body().string();
                        Gson gson = new Gson();
                        final Detailed detailed = gson.fromJson(str,Detailed.class);
                        Log.e("data",str);
                        if (detailed.getStatus() == "has not signup"  || detailed.getStatus().length() != 0){
                            Intent intent = new Intent(context, SignInActivity.class);
                            startActivity(intent);
                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ToastUtil.makeShortToast(context,"留言成功");
                                    Intent intent = new Intent();
                                    intent.putExtra("data",detailed);
                                    setResult(2,intent);
                                    finish();
                                }
                            });

                        }
                    }
                });
            }
        }).start();

        /*Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Test test = retrofit.create(Test.class);
        retrofit2.Call<Detailed> detailedCall = test.DETAILED_CALL(productId
                ,UserUtil.getCurrentUser(context).getId()
                ,content);
        detailedCall.enqueue(new retrofit2.Callback<Detailed>() {
            @Override
            public void onResponse(retrofit2.Call<Detailed> call, retrofit2.Response<Detailed> response) {
                if (response.isSuccessful()){
                    Log.e("data","留言成功");
                    Log.e("data",response.body().getStatus());
                }
            }

            @Override
            public void onFailure(retrofit2.Call<Detailed> call, Throwable t) {

            }
        });*/

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.activity_bottom_dialogbox_send:
                break;
            case R.id.activity_bottom_dialogbox_edit:
                break;
            default:
                finish();
                break;
        }
    }
}
